//
//  YewenAdapter.m
//  jobClient
//
//  Created by 小细菌 on 2020/9/29.
//  Copyright © 2020 YL1001. All rights reserved.
//

#import "YewenAdapter.h"
#import "ELTrainingRequestModel.h"
#import "ELTrainingClassDetailViewController.h"
#import "TrainingWorksList.h"
#import "ELTrainingClassAudioViewController.h"
#import "YewenCollegeCtrl.h"
#import "YewenLivingCtrl.h"
#import "GuangZhouTrainingCtrl.h"
@implementation YewenAdapter



+ (void)getStudyConfig:(NSString*)project_id type:(NSString*)type complete:(void(^)(BOOL ok , NSString* errorMsg))complete{
   if(type.isNull)
       type = @"yewen_college";
    ELTrainingRequestModel *model = [[ELTrainingRequestModel alloc] init];
    [model getProjectConfig:project_id type:type];
    [BaseUIViewController showLoadView:YES content:@"" view:nil];
    [HPNetworking postWithModel:model succeed:^(id _Nullable result) {
        [BaseUIViewController showLoadView:NO content:nil view:nil];
        NSLog(@"---%@",result);
        if ([result[@"code"] intValue] == 475) {
            APPCONFIG.training.lackIdCheck = YES;
            APPCONFIG.training.lackIdCheckMsg = result[@"status_desc"];
            complete(NO , nil);
            return;
        }else {
            APPCONFIG.training.lackIdCheck = NO;
        }
        
        if ([result[@"code"] intValue] == 200) {
            [AppConfigServer shareInstance].appConfig.training = [TrainingConfigModel yy_modelWithDictionary:result[@"data"]];
            APPCONFIG.training.name = result[@"data"][@"check_person_name"];
            APPCONFIG.training.carNum = result[@"data"][@"check_person_idcard"];
            APPCONFIG.training.needIdCheck = [NSString stringWithFormat:@"%@",result[@"data"][@"is_need_check_image_recognition"]];
            
            if ([result[@"data"][@"is_face_auth"] intValue] == 1) {
                //[[Manager shareMgr].industryAskedCtl doFaceRecognition];
                APPCONFIG.training.is_face_auth = YES;
                complete(NO , nil);
                return;
            }
            APPCONFIG.training.is_face_auth = NO;
            if(APPCONFIG.training.trainingStatus == 2){
                ELRequestModel *requestModel = [[ELRequestModel alloc] init];
                [requestModel getPersonInfo];
                [ELRequest postWithModel:requestModel showProgress:YES progressName:nil success:^(NSURLSessionDataTask *operation, id result) {
                    NSDictionary *resultDic = result;
                    if (DictIsNotEmpty(resultDic)) {
                        NSString *code = [resultDic objectForKey:@"code"];
                        if ([code isEqualToString:@"200"]) {
                            APPCONFIG.training.name = resultDic[@"data"][@"person_name"];
                            APPCONFIG.training.carNum = resultDic[@"data"][@"person_idcard"];
                            APPCONFIG.training.cartType = [resultDic[@"data"][@"cert_type"] longValue];
                            APPCONFIG.training.emp_id = resultDic[@"data"][@"emp_id"];

                            complete(YES , nil);
                        }else{
                            complete(NO , nil);
                            [BaseUIViewController showAutoDismissFailView:nil msg:resultDic[@"status_desc"]];
                        }
                    }
                    else {
                        complete(NO , @"数据异常");
                        [BaseUIViewController showAutoDismissFailView:nil msg:@"数据异常"];
                    }
                } failure:^(NSURLSessionDataTask *operation, NSError *error) {
                    complete(NO , @"数据异常");
                }];
            
            }else{
                complete(YES , nil);
            }
            
        }else{
            complete(NO , result[@"status_desc"]);
        }
    } failure:^(NSString * _Nullable error) {
        [BaseUIViewController showLoadView:NO content:nil view:nil];
        complete(NO , @"网络异常");
        [BaseUIViewController showAutoDismissFailView:nil msg:@"网络异常"];
    }];
}


+ (void)gotoYewenCoursList:(NSString*)group_id type:(NSString*)type project_id:(NSString*)project_id{
    
    [self getStudyConfig:project_id type:type complete:^(BOOL ok , NSString* msg){
        if(ok){
            ELTrainingClassDetailViewController *detailVc = [[ELTrainingClassDetailViewController alloc] init];
            detailVc.group_id = group_id;
            detailVc.type = type;
            [[Manager getCurrentVC].navigationController pushViewController:detailVc animated:YES];
        }
    }];

}


+ (void)gotoShenZhenPlay:(NSString*)group_id type:(NSString*)type project_id:(NSString*)project_id{
    APPCONFIG.training.projectId = project_id;
    ELRequestModel *requestModel = [[ELRequestModel alloc] init];
    [requestModel getGroupSectionWorks:group_id];
    [ELRequest postWithModel:requestModel showProgress:YES progressName:nil success:^(NSURLSessionDataTask *operation, id result) {
        NSLog(@"%@",result);
        NSString *code = result[@"code"];
        if (code.intValue == 200) {
            TrainingWorksList* workslist = [TrainingWorksList yy_modelWithDictionary:result[@"data"]];
            
            ELTrainingClassAudioViewController * detailVC = [[ELTrainingClassAudioViewController alloc] initWithAsset:nil];
            detailVC.group_id = group_id;
            detailVC.article_id = workslist.play_works_id;
            detailVC.playType = PlayTypeEnterpriseTraining;
            detailVC.listModel = workslist;
            [[Manager shareMgr].centerNav_ pushViewController:detailVC animated:YES];
        }else{
            toast(result[@"status_desc"]);
        }
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        NSLog(@"---%@",error);
        
    }];

}

+ (void)gotoShenZhenPlay:(NSString*)group_id project_id:(NSString*)project_id article_id:(NSString*)article_id{
    APPCONFIG.training.projectId = project_id;
    ELRequestModel *requestModel = [[ELRequestModel alloc] init];
    [requestModel getGroupSectionWorks:group_id];
    [ELRequest postWithModel:requestModel showProgress:YES progressName:nil success:^(NSURLSessionDataTask *operation, id result) {
        NSLog(@"%@",result);
        NSString *code = result[@"code"];
        if (code.intValue == 200) {
            TrainingWorksList* workslist = [TrainingWorksList yy_modelWithDictionary:result[@"data"]];
            
            ELTrainingClassAudioViewController * detailVC = [[ELTrainingClassAudioViewController alloc] initWithAsset:nil];
            detailVC.group_id = group_id;
            detailVC.article_id = workslist.play_works_id = article_id;
            detailVC.playType = PlayTypeEnterpriseTraining;
            detailVC.listModel = workslist;
            [[Manager shareMgr].centerNav_ pushViewController:detailVC animated:YES];
        }else{
            toast(result[@"status_desc"]);
        }
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        NSLog(@"---%@",error);
    }];
}

+ (void)gotoYeWenPlay:(NSString*)course_id task_id:(NSString*)task_id project_id:(NSString*)project_id fromKnowledge:(BOOL)fromKnowledge{
    APPCONFIG.training.projectId = project_id;
    ELTrainingRequestModel *model = [[ELTrainingRequestModel alloc] init];
    [model getCourseSectionWorks:course_id task_id:task_id];
    HudShow;
    [HPNetworking postWithModel:model succeed:^(id _Nullable result) {
        NSLog(@"---%@",result);
        HudHide;
        if ([result[@"code"] intValue] == 200) {
            TrainingWorksList* workslist = [TrainingWorksList yy_modelWithDictionary:result[@"data"]];
            YewenCollegeViewModel* viewModel = YewenCollegeViewModel.new;
            viewModel.article_id = workslist.play_works_id;
            viewModel.group_id = course_id;
            viewModel.isComplete = fromKnowledge;
            YewenCollegeCtrl* vc = [[YewenCollegeCtrl alloc] initWithViewModel:viewModel];
            vc.worksList = workslist;
            vc.projectId = project_id;
            vc.isfromKnowledge = fromKnowledge;
            [[Manager shareMgr].centerNav_ pushViewController:vc animated:YES];
        }else{
            toast(result[@"status_desc"]);
        }
    } failure:^(NSString * _Nullable error) {
        HudHide;
        NSLog(@"---%@",error);
        toast(@"网络异常");
    }];

    
    
    
}

+ (void)gotoYeWenPlay:(NSString*)course_id task_id:(NSString*)task_id project_id:(NSString*)project_id article_id:(NSString*)article_id{
    if(![task_id isKindOfClass:NSString.class]){
        task_id = GXString(@"%@",task_id);
    }
    
    APPCONFIG.training.projectId = project_id;
    ELTrainingRequestModel *model = [[ELTrainingRequestModel alloc] init];
    [model getCourseSectionWorks:course_id task_id:task_id];
    [HPNetworking postWithModel:model succeed:^(id _Nullable result) {
        NSLog(@"---%@",result);
        if ([result[@"code"] intValue] == 200) {
            TrainingWorksList* workslist = [TrainingWorksList yy_modelWithDictionary:result[@"data"]];
            
            ELTrainingClassAudioViewController * detailVC = [[ELTrainingClassAudioViewController alloc] initWithAsset:nil];
            detailVC.group_id = course_id;
            detailVC.article_id = workslist.play_works_id = article_id;
            detailVC.playType = PlayTypeEnterpriseTraining;
            detailVC.listModel = workslist;
            [[Manager shareMgr].centerNav_ pushViewController:detailVC animated:YES];
        }else{
            toast(result[@"status_desc"]);
        }
    } failure:^(NSString * _Nullable error) {
        NSLog(@"---%@",error);
        toast(@"网络异常");
    }];

}


+ (void)gotoGuangZhouPlay:(NSString*)course_id task_id:(NSString*)task_id project_id:(NSString*)project_id article_id:(NSString*)article_id{
    APPCONFIG.training.projectId = project_id;
    ELTrainingRequestModel *model = [[ELTrainingRequestModel alloc] init];
    [model getCourseSectionWorks:course_id task_id:task_id];
    [HPNetworking postWithModel:model succeed:^(id _Nullable result) {
        NSLog(@"---%@",result);
        if ([result[@"code"] intValue] == 200) {
            TrainingWorksList* workslist = [TrainingWorksList yy_modelWithDictionary:result[@"data"]];
            
            GuangZhouViewModel* viewModel = GuangZhouViewModel.new;
            viewModel.works_id = StringIsEmpty(article_id)?workslist.play_works_id:article_id;
            viewModel.project_id = APPCONFIG.training.projectId;
            viewModel.task_id = APPCONFIG.training.taskId;
            viewModel.course_id = course_id;
            GuangZhouTrainingCtrl* vc = [[GuangZhouTrainingCtrl alloc] initWithViewModel:viewModel];
            vc.workslist = workslist;
            [[Manager shareMgr].centerNav_ pushViewController:vc animated:YES];
        }else{
            toast(result[@"status_desc"]);
        }
    } failure:^(NSString * _Nullable error) {
        NSLog(@"---%@",error);
        toast(@"网络异常");
    }];
}

+ (void)gotoYewenLivingWithId:(NSString*)livingId groupId:(NSString*)groupId yewenLive:(BOOL)isYewenLive {
    YewenLivingViewModel* viewModel = YewenLivingViewModel.new;
    viewModel.sub_live_id = livingId;
    viewModel.group_id = groupId;
    viewModel.isYewenLiving = isYewenLive;
    viewModel.project_id = [APPCONFIG.training.projectId intValue]>0?APPCONFIG.training.projectId:@"0";
    viewModel.task_id = [Manager getUserInfo].taskId;
    YewenLivingCtrl* vc = [[YewenLivingCtrl alloc] initWithViewModel:viewModel];
    [[Manager shareMgr].centerNav_ pushViewController:vc animated:YES];
}




+ (void)gotoHeNanWithCourseId:(NSString*)courseId name:(NSString *)courseName projectId:(NSString*)pid worksId:(NSString*)worksId {
    APPCONFIG.training.projectId = pid;
    /*
     
                personId  23631432   √
     worksId  1641631615415852   // 视频类的才需要传  可能是article_id
                courseId  6681651716451337  √course_id
                courseName   测试课程
                playProcess  0   √
                companyId   cm1293527382304  √
                projectId 7961  √ project_id
     

     */
    ELTrainingRequestModel *model = [[ELTrainingRequestModel alloc] init];
    [model getHeNanTokenWithCourseId:courseId courseName:courseName projectId:pid worksId:worksId process:@"0"];
    [HPNetworking postWithModel:model succeed:^(id _Nullable result) {
        NSLog(@"---%@",result);
//        if ([result[@"code"] intValue] == 200) {
//            TrainingWorksList* workslist = [TrainingWorksList yy_modelWithDictionary:result[@"data"]];
//            
//            GuangZhouViewModel* viewModel = GuangZhouViewModel.new;
//            viewModel.works_id = StringIsEmpty(article_id)?workslist.play_works_id:article_id;
//            viewModel.project_id = APPCONFIG.training.projectId;
//            viewModel.task_id = APPCONFIG.training.taskId;
//            viewModel.course_id = course_id;
//            GuangZhouTrainingCtrl* vc = [[GuangZhouTrainingCtrl alloc] initWithViewModel:viewModel];
//            vc.workslist = workslist;
//            [[Manager shareMgr].centerNav_ pushViewController:vc animated:YES];
//        }else{
//            toast(result[@"status_desc"]);
//        }
    } failure:^(NSString * _Nullable error) {
        NSLog(@"---%@",error);
        toast(@"网络异常");
    }];
}

@end
